package com.china08.yxyapi.api.manage.tec.teaching.lecture.teachreport;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.esbag.YxyEsbagTeachingReport;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.esbag.YxyEsbagTeachingReportRepository;

/**
 * 老师 ---备授课--授课报告-授课包查询
 * 
 */
@CrossOrigin
@RestController
public class LectureReportClassSrhController extends YxyApiBaseController {

    @Autowired
    private YxyEsbagTeachingReportRepository yxyEsbagTeachingReportRepository;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/lecture/teachreport/class/sch", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Page<RespModel> process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        String classId = reqModel.classId;
        String keyword = reqModel.keyword;
        String bookContentId = reqModel.bookContentId;
        List<String> classIdList = reqModel.classIdList;
        Assert.notNull(StringUtils.defaultIfBlank(classId, null), "班级ID为空");

        Pageable pageable = reqModel.getPageable(Direction.DESC, "preDate");
        String userId = getProfile(request, response).getId();
        String schoolId = getProfile(request, response).getSchoolId();
        Page<YxyEsbagTeachingReport> page = null;
        if (StringUtils.isNotBlank(keyword)) {
            if ("all".equals(classId)) {
                page = yxyEsbagTeachingReportRepository
                        .findByTeacherIdAndSchoolIdAndClassIdInAndBookcontentsIdAndPackNameContaining(userId, schoolId,
                                classIdList, bookContentId, keyword, pageable);
            } else {
                page = yxyEsbagTeachingReportRepository
                        .findByTeacherIdAndSchoolIdAndClassIdAndBookcontentsIdAndPackNameContaining(userId, schoolId,
                                classId, bookContentId, keyword, pageable);
            }

        } else {
            if ("all".equals(classId)) {
                page = yxyEsbagTeachingReportRepository.findByTeacherIdAndSchoolIdAndClassIdInAndBookcontentsId(userId,
                        schoolId, classIdList, bookContentId, pageable);
            } else {
                page = yxyEsbagTeachingReportRepository.findByTeacherIdAndSchoolIdAndClassIdAndBookcontentsId(userId,
                        schoolId, classId, bookContentId, pageable);

            }
        }

        List<RespModel> list = new ArrayList<RespModel>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for (YxyEsbagTeachingReport yxyEsbagTeachingReport : page) {
            RespModel respModel = new RespModel();
            respModel.id = yxyEsbagTeachingReport.getId();
            respModel.classId = yxyEsbagTeachingReport.getClassId();
            respModel.className = yxyEsbagTeachingReport.getClassName();
            respModel.packName = yxyEsbagTeachingReport.getPackName();
            Date preDate = yxyEsbagTeachingReport.getLastModifiedDate();
            respModel.preDate = sdf.format(preDate);
            respModel.teacherNick = yxyEsbagTeachingReport.getTeacherNick();
            respModel.userTime = yxyEsbagTeachingReport.getUserTime();
            list.add(respModel);
        }
        Page<RespModel> pageRes = new PageImpl<RespModel>(list, pageable, page.getTotalElements());

        return pageRes;
    }

    protected static class ReqModel extends YxyApiPageReqModel {
        // 班级id
        public String classId;

        public String keyword;

        public List<String> classIdList;
        // 目录ID
        public String bookContentId;
    }

    protected static class RespModel extends YxyApiRespModel {

        // 授课包id
        public String id;
        // 授课包名称
        public String packName;
        // 授课包教师名称
        public String teacherNick;
        // 授课时间
        public String preDate;
        // 授课时长
        public Long userTime;
        // 授课班级ID
        public String classId;
        // 授课班级名称
        public String className;

    }
}
